package com.fzj.example.jdk18;


import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * ClassName: FunctionInterfaceDemo
 * date: 2019/11/12 15:05
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class FunctionInterfaceDemo {

    //->都需要理解成抽象方法的具体实现，也就是该函数式接口抽象方法的具体实现
    public static void main(String[] args) {
        /******************Consumer：业务范围：日志打印，计算结果***********************/
        con(x->System.out.println(x),"Consumer：日志打印：数据库连接异常");
        /******************Supplier：业务范围：报表数据计算，统计***********************/
        Map<Integer,Double> sales=new HashMap<>();
        sales.put(12,1.44);
        sales.put(13,1.42);
        sales.put(13,11.42);
        double sum=0.0d;
        for (Map.Entry<Integer,Double> map: sales.entrySet()) {
            Supplier<Double> supplier=()->map.getKey()*map.getValue();
            sum+=supplier.get();
        }
        System.out.println("Supplier：总价(元):"+sum);
        /*****************Function:业务范围：出参和入参存在关联***********************/
        //字符串大写化
        uperCase((x)->{
            System.out.println("Function：字符串大写"+x.toUpperCase());
            return x.toUpperCase();},"fzj");
        /*****************Predicate:业务范围：过滤和测试***********************/
        //齐格人数的平均分数
        List<Integer> list = Arrays.asList(66,100,26,63,68,75,89,92,56);
        pre(m->m>=60,list);
    }
    public static void con(Consumer<String> consumer, String data) {
        consumer.accept(data);
    }
    public static String uperCase(Function<String,String> function, String data) {
        return  function.apply(data);
    }
    public static void pre(Predicate<Integer> predicate,List<Integer> list) {
        int num=0;
        int sum=0;
        for (Integer m:list) {
            if(predicate.test(m)){
                sum+=m;
                num++;
            }
        }
        System.out.println("Predicate：平均分数:"+sum/num);
    }
}
